package com.xps.charmingshop.view.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.xps.charmingshop.R;
import com.xps.charmingshop.bean.CompleteBean;
import com.xps.charmingshop.bean.ConductBean;
import com.xps.charmingshop.date.net.UserNetWorks;
import com.xps.charmingshop.utils.SharedPreferencesUtils;
import com.xps.charmingshop.view.activity.BillActivity;
import com.xps.charmingshop.view.activity.CompleteActivity;
import com.xps.charmingshop.view.activity.ConductActivity;
import com.xps.charmingshop.view.activity.LoginActivity;
import com.xps.charmingshop.view.activity.NewsActivity;
import com.xps.charmingshop.view.activity.ShopActivity;
import com.xps.charmingshop.view.base.BaseFragment;
import com.zhy.autolayout.AutoLinearLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import rx.Subscriber;


/**
 * Created by MI on 2018/1/10.
 */

public class ListFragment extends BaseFragment {
    @BindView(R.id.layout_News)
    AutoLinearLayout layoutNews;
    Unbinder unbinder;
    @BindView(R.id.Layout_Shop)
    AutoLinearLayout LayoutShop;
    private static final String PREFS_NAME = "MyUserInfo";
    @BindView(R.id.Red_MeOrder)
    TextView RedMeOrder;
    @BindView(R.id.Red_List)
    TextView RedList;
    @BindView(R.id.Red_News)
    TextView RedNews;

    @Override
    protected int getRes() {
        return R.layout.fragment_list;
    }

    @Override
    protected void init(View view) {
        unbinder = ButterKnife.bind(this, view);
        doMore();
        doMoreList();
    }

    @Override
    protected void setData(View view) {

    }

    @Override
    public void onClick(View v) {

    }

    public void hide() {
        FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.hide(this);
        fragmentTransaction.commit();
    }

    public void doMore() {
        String user_token = SharedPreferencesUtils.getString("user_token", "String", null);
        UserNetWorks.getOrder(user_token, new Subscriber<ConductBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
            }

            @Override
            public void onNext(ConductBean tongZhiBean) {
                List<ConductBean.ReturnBodyBean> list = tongZhiBean.getReturn_body();
                if (list != null && list.size() > 0) {
                    for (int i = 0; i < list.size(); i++) {
                        RedMeOrder.setText((i + 1) + "");
                    }
                } else {
                    RedMeOrder.setText("0");
                }

            }
        });
    }

    public void doMoreList() {
        String user_token = SharedPreferencesUtils.getString("user_token", "String", null);
        UserNetWorks.getComplete(user_token, "-1", new Subscriber<CompleteBean>() {

            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
            }

            @Override
            public void onNext(CompleteBean completeBean) {
                List<CompleteBean.ReturnBodyBean> list = completeBean.getReturn_body();
                if (list != null && list.size() > 0) {
                    for (int i = 0; i < list.size(); i++) {
                        RedList.setText((i + 1) + "");
                    }
                } else {
                    RedList.setText("0");
                }
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        doMoreList();
        doMore();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.layout_News, R.id.Layout_List, R.id.Layout_Shop, R.id.layout_TC, R.id.Layout_MeOrder, R.id.Layour_Bill})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.layout_News:
                Intent intent = new Intent(getActivity(), NewsActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                getActivity().startActivity(intent);
                break;
            case R.id.Layout_List:
                Intent intent1 = new Intent(getActivity(), CompleteActivity.class);
                intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                getActivity().startActivity(intent1);
                break;
            case R.id.Layout_Shop:
                Intent intent2 = new Intent(getActivity(), ShopActivity.class);
                intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                getActivity().startActivity(intent2);
                break;
            case R.id.Layout_MeOrder:
                Intent intent4 = new Intent(getActivity(), ConductActivity.class);
                intent4.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                getActivity().startActivity(intent4);
                break;
            case R.id.Layour_Bill:
                Intent intent5 = new Intent(getActivity(), BillActivity.class);
                intent5.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                getActivity().startActivity(intent5);
                break;
            case R.id.layout_TC:
                Intent intent3 = new Intent(getActivity(), LoginActivity.class);
                intent3.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                SharedPreferencesUtils.putBoolean("isSave", "boolean", false);
                SharedPreferencesUtils.putString("name", "String", "");
                SharedPreferencesUtils.putString("password", "String", "");
                getActivity().startActivity(intent3);
                getActivity().finish();
                break;
        }
    }
}
